package com.heima.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.domain.Department;
import com.heima.mapper.DepartmentMapper;
import com.heima.service.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.vo.DepartmentVO;
import com.heima.vo.DepartmentchildVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 科室 服务实现类
 * </p>
 *
 * @author wangjiexin
 * @since 2023-01-29
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {
    @Autowired
    DepartmentMapper departmentMapper;



    @Override
    public List<DepartmentVO> selectList(Integer page, Integer pageSize) {
        // 一级科室列表 -> child -> 二级科室列表

        // 1. 查出所有一级科室列表
        // 参数一 page分页规则
        // 参数二 查询规则
        IPage<Department> pageInfo=new Page<>();
        //IPage<Department> pageInfo = new Page<>();

        LambdaQueryWrapper<Department> wrapper = new LambdaQueryWrapper<>();
        // 1号医院下的所有可用科室
        wrapper.eq(Department::getHospitalId, 1);
        wrapper.eq(Department::getState, true);

        IPage<Department> deptList = departmentMapper.selectPage(pageInfo, wrapper);

        if (deptList == null || CollectionUtil.isEmpty(deptList.getRecords())) {
            return new ArrayList<>();
        }

        // 2. 提取所有医技科室列表id
        List<Department> records = deptList.getRecords();
        List<Integer> deptIdList = records.stream().filter(Objects::nonNull).map(Department::getId).collect(Collectors.toList());

        // 3. 根据一级科室列表id，查出所有二级科室列表
        LambdaQueryWrapper<Department> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(Department::getParent, deptIdList);
        wrapper.eq(Department::getState, true);
        List<Department> subDeptList = departmentMapper.selectList(wrapper1);

        // 4. 一级科室和二级科室 对应组装成vo返回
        // 创建一个返回结构 List<DepartmentVo>
        List<DepartmentVO> voList = new ArrayList<>();

        if(!CollectionUtil.isEmpty(subDeptList)) {
            // 遍历一级科室列表
            for (Department record : records) {
                if(record == null) {
                    continue;
                }

                DepartmentVO vo = new DepartmentVO();

                // 每次遍历，提取出当前一级科室列表的id
                Integer id = record.getId();

                // 声明一个集合，用来存储下级所有二级科室的信息
                List<DepartmentchildVO> childVoList = new ArrayList<>();

                // 遍历公共的二级科室集合，在里面找到和我当前一级科室匹配的二级科室
                for (Department hhDepartment : subDeptList) {
                    if(hhDepartment == null) {
                        continue;
                    }

                    Integer parent = hhDepartment.getParent();
                    if(id.equals(parent)) {
                        // 把匹配的二级科室信息，拷贝到vo中，并且把vo加入到下级科室集合
                        DepartmentchildVO childVo = new DepartmentchildVO();
                        BeanUtils.copyProperties(hhDepartment, childVo);
                        childVoList.add(childVo);
                    }
                }

                // 遍历完后，下级科室列表就收集完成了
                vo.setChild(childVoList);
                vo.setId(id);
                vo.setName(record.getName());

                // 把一级和二级组装到一个vo，并加入到list
                voList.add(vo);
            }
        }

        return voList;


    }
}
